package com.mc.web.controller.system;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.mc.common.annotation.Log;
import com.mc.common.core.controller.BaseController;
import com.mc.common.core.domain.AjaxResult;
import com.mc.common.enums.BusinessType;
import com.mc.system.domain.SysPlant;
import com.mc.system.service.ISysPlantService;
import com.mc.common.utils.poi.ExcelUtil;
import com.mc.common.core.page.TableDataInfo;

/**
 * 植物字典 Controller
 *
 * @author mc
 * @date 2021-04-06
 */
@RestController
@RequestMapping("/system/plant")
public class SysPlantController extends BaseController
{
    @Autowired
    private ISysPlantService sysPlantService;

    /**
     * 查询植物字典 列表
     */
    @PreAuthorize("@ss.hasPermi('system:plant:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysPlant sysPlant)
    {
        startPage();
        List<SysPlant> list = sysPlantService.selectSysPlantList(sysPlant);
        return getDataTable(list);
    }

    /**
     * 查询植物字典 列表
     */
    @PreAuthorize("@ss.hasPermi('system:plant:list')")
    @GetMapping("/list/by/{name}")
    public AjaxResult listByname(@PathVariable("name") String name)
    {
        SysPlant sysPlant = new SysPlant();
        sysPlant.setName(name);
        List<SysPlant> list = sysPlantService.selectSysPlantList(sysPlant);
        return AjaxResult.success(list);
    }

    /**
     * 查询植物字典 列表
     */
    @PreAuthorize("@ss.hasPermi('system:plant:list')")
    @GetMapping("/list/qualification/{type}")
    public AjaxResult qualification(@PathVariable("type") String type)
    {
        if(type.equals("family")){
            return AjaxResult.success(sysPlantService.selectFamily());
        }else if(type.equals("district")){
            return AjaxResult.success(sysPlantService.selectDistrict());
        }else{
            return AjaxResult.error("类型不存在");
        }
    }

    /**
     * 导出植物字典 列表
     */
    @PreAuthorize("@ss.hasPermi('system:plant:export')")
    @Log(title = "植物字典 ", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysPlant sysPlant)
    {
        List<SysPlant> list = sysPlantService.selectSysPlantList(sysPlant);
        ExcelUtil<SysPlant> util = new ExcelUtil<SysPlant>(SysPlant.class);
        return util.exportExcel(list, "plant");
    }

    /**
     * 获取植物字典 详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:plant:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysPlantService.selectSysPlantById(id));
    }

    /**
     * 新增植物字典
     */
    @PreAuthorize("@ss.hasPermi('system:plant:add')")
    @Log(title = "植物字典 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysPlant sysPlant)
    {
        return toAjax(sysPlantService.insertSysPlant(sysPlant));
    }

    /**
     * 修改植物字典
     */
    @PreAuthorize("@ss.hasPermi('system:plant:edit')")
    @Log(title = "植物字典 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysPlant sysPlant)
    {
        return toAjax(sysPlantService.updateSysPlant(sysPlant));
    }

    /**
     * 删除植物字典
     */
    @PreAuthorize("@ss.hasPermi('system:plant:remove')")
    @Log(title = "植物字典 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysPlantService.deleteSysPlantByIds(ids));
    }
}